﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SGUI;
using DevComponents.DotNetBar.SuperGrid;
using SDLL;

namespace SABI
{
    public partial class frm0181 : Form
    {
        public frm0181()
        {
            InitializeComponent();
        }

        private int TabID = 1;
        private string sql = "";
        private DataTable dt_Grid1 = new DataTable();
        string _VoucherID, _KindVoucherID;

        public void AddTab(Form frm, bool closebtnVisible)
        {
            SuperTabItem newtab = TabMain.CreateTab("Tab");
            frm.TopLevel = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();
            newtab.Name = frm.Name+TabID.ToString();
            newtab.Text = string.Format("[{0}] {1}",TabID,frm.Text);
            newtab.CloseButtonVisible = closebtnVisible;
            newtab.AttachedControl.Controls.Add(frm);          
            TabMain.SelectedTabIndex = TabMain.Tabs.Count - 1;
            TabID += 1;
        }

        private void frm0256_Load(object sender, EventArgs e)
        {
            LANG.TextForm(this, FRM.Lang);
            //loadDATA_Combo();
            //loadDATA_Grid();
        }

        void loadDATA_Combo()
        {
            // Kỳ kế toán
            sql = "SELECT DISTINCT";
            sql += " MonthYear , TranMonth , TranYear FROM vi0317 WHERE DivisionID = '" + FRM.DivisionID + "' AND CHARINDEX('"+FRM.ID_Group+"',GroupShow)>0";
            sql += " ORDER BY TranYear DESC, TranMonth DESC";
            DataTable dt_Period = FRM.GetDataTable(sql);
            TDBCboFromPeriod.DataSource = dt_Period;
            TDBCboFromPeriod.SelectedValue = FRM.Period;
            TDBCboToPeriod.DataSource = dt_Period;
            TDBCboToPeriod.SelectedValue = FRM.Period;

            DTPFromDate.Value = FRM.DateDue;
            DTPToDate.Value = FRM.DateDue;
            GUI.ChangeDateFormat(DTPFromDate, FRM.SystemDateFormat);
            GUI.ChangeDateFormat(DTPToDate, FRM.SystemDateFormat);
        }

        public void loadDATA_Grid()
        {
            if(TDBCboFromPeriod.SelectedItem==null||TDBCboToPeriod.SelectedItem==null){return;}

            sql = string.Format("pr0003 '{0}',{1},{2},'','',{3},{4}", FRM.DivisionID, TDBCboFromPeriod.SelectedItem.ItemData[1], TDBCboFromPeriod.SelectedItem.ItemData[2], TDBCboToPeriod.SelectedItem.ItemData[1], TDBCboToPeriod.SelectedItem.ItemData[2]);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString, true);
            
            sql = "SELECT A.*,B.ShipperName from vi0000 A ";
            sql += "Left Join(SELECT ObjectID AS ShipperID,ObjectName as ShipperName FROM tb0045 UNION ALL SELECT EmployeeID as ShipperID,FullName as ShipperName FROM tb0040) B ON A.ShipperID=B.ShipperID ";
            sql += "LEFT JOIN tb0071 ON A.CreateUserID = tb0071.UserID ";
            sql += "LEFT JOIN tb0070 ON tb0070.GroupID = tb0071.GroupID ";
            sql += string.Format("Where DivisionID = '{0}' AND CHARINDEX('@'+ CAST(tb0070.ID AS VARCHAR(20)),'{1}')>0 ", FRM.DivisionID, FRM.ID_GroupList);

            if (optFromPeriod.Checked)
            {
                sql += " AND TranMonth+TranYear*100 BETWEEN " + (int.Parse(TDBCboFromPeriod.SelectedItem.ItemData[1]) + int.Parse(TDBCboFromPeriod.SelectedItem.ItemData[2]) * 100).ToString();
                sql += " AND " + (int.Parse(TDBCboToPeriod.SelectedItem.ItemData[1]) + int.Parse(TDBCboToPeriod.SelectedItem.ItemData[2]) * 100).ToString();
            }
            else
            {
                sql += " AND VoucherDate BETWEEN '" + DTPFromDate.Value.ToShortDateString() + "' AND '" + DTPToDate.Value.ToShortDateString() + "'";
            }
            sql += " Order by VoucherTypeID,VoucherDate, VoucherNo";
            
            dt_Grid1 = FRM.GetDataTable(sql);
            //DFORMAT.NumberFormat(ref dt_Grid1, new string[] { "OriginalAmount", "ConvertedAmount", "TaxAmount" }, '.');
            DBRender.GridDB(GridDB1, dt_Grid1);
            Application.DoEvents();
        }

        private void TDBCboFromPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (optFromPeriod.Checked) { loadDATA_Grid(); }
        }

        private void TDBCboToPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (optFromPeriod.Checked) { loadDATA_Grid(); }
        }

        private void optFromPeriod_CheckedChanged(object sender, EventArgs e)
        {
            loadDATA_Grid();
        }

        private void DTPFromDate_ValueChanged(object sender, EventArgs e)
        {
            if (optFromDate.Checked) { loadDATA_Grid();}
        }

        private void DTPToDate_ValueChanged(object sender, EventArgs e)
        {
            if (optFromDate.Checked) { loadDATA_Grid(); }
        }

        private void GridDB1_SelectionChanged(object sender, GridEventArgs e)
        {
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            if (gr == null)
            {
                _VoucherID = "";
                _KindVoucherID = "";
                return;
            }
            _VoucherID = gr["clVoucherID"].Value.ToString();
            _KindVoucherID = gr["clKindVoucherID"].Value.ToString();
            GetDetail(_VoucherID);
            RowUpdate();
        }

        void GetDetail(string VoucherID)
        {
            sql = string.Format("SELECT * FROM vi0019 WHERE VoucherID = '{0}'", VoucherID);

            DataTable dt_Grid2 = FRM.GetDataTable(sql);
            
            DBRender.GridDB(GridDB2, dt_Grid2);
            GridDB2.PrimaryGrid.Footer.Text = String.Format("Tổng số dòng: {0}", dt_Grid2.Rows.Count);
        }

        public void CloseTab()
        {
            if (TabMain.Tabs.Count > 0){ TabMain.Tabs.RemoveAt(TabMain.SelectedTabIndex); }
        }

        private void frm0256_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            optFromPeriod.Checked = true;
            RowUpdate();
        }

        private void GridDB1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {
            Form frm = new frm0222(false, false, _VoucherID);
            frm.ShowDialog();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            if(_KindVoucherID=="1")
            {
                Form frm = new frm0188(false, false, _VoucherID);
                frm.ShowDialog();
            }            
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
           // ....
           
        }

        private void mView_Click(object sender, EventArgs e)
        {
            Form frm = new frm0188(false, true, _VoucherID);
            frm.ShowDialog();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            string tn = "tb0104";
            string ws = string.Format("WHERE VoucherID='{0}'", _VoucherID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            loadDATA_Grid();
        }

        public void RowUpdate()
        {
            GridDB1.Update();
            if (GridDB1.PrimaryGrid.Rows.Count == 0)
            {
                mEdit.Enabled = false;
                mView.Enabled = false;
                mDelete.Enabled = false;
                mDuplicate.Enabled = false;
                GridDB2.PrimaryGrid.Rows.Clear();
                GridDB1.PrimaryGrid.Footer.Text = null;
            }
            else
            {
                mEdit.Enabled = true;
                mView.Enabled = true;
                mDelete.Enabled = true;
                mDuplicate.Enabled = true;
                GridDB1.PrimaryGrid.Footer.Text = String.Format("Tổng số dòng: {0:#,#}", GridDB1.PrimaryGrid.Rows.Count);
            }
        }

        private void mDuplicate_Click(object sender, EventArgs e)
        {
            // ...
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHOME"] as frmHOME).CloseTab(this);
        }

        private void mImport_Click(object sender, EventArgs e)
        {
            AddTab(new frm0188(), true);
        }

        private void mExport_Click(object sender, EventArgs e)
        {

        }

        private void mTransfer_Click(object sender, EventArgs e)
        {

        }

        

    }
}
